package com.naiterui.ehp.bs.emr.utils;

import org.apache.commons.lang3.StringUtils;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.emr.exception.ExceptionCodes;
import com.naiterui.ehp.bs.emr.vo.LimitConfigVO;

/**
 * 客户端极限值配置Config
 *

 * @since 2.5.0
 */
public class LimitValueConfig {

    // 患者名称长度限制
    public static final String LIMIT_PATIENT_NAME = "limit.patient.name";
    // 患者年龄长度限制
    public static final String LIMIT_PATIENT_AGE = "limit.patient.age";
    // 主诉
    public static final String LIMIT_DISEASE_MAINCOMPLAINT = "limit.disease.mainComplaint";
    // 现病史
    public static final String LIMIT_DISEASE_PRESENTDISEASE = "limit.disease.presentDisease";
    // 既往史
    public static final String LIMIT_DISEASE_PAST = "limit.disease.past";
    // 体温
    public static final String LIMIT_DISEASE_TEMPERATURE = "limit.disease.temperature";
    // 体重
    public static final String LIMIT_DISEASE_WEIGHT = "limit.disease.weight";
    // 心率
    public static final String LIMIT_DISEASE_HEARTRETE = "limit.disease.heartRete";
    // 收缩压
    public static final String LIMIT_DISEASE_SYSTOLIC = "limit.disease.systolic";
    // 舒张压
    public static final String LIMIT_DISEASE_DIASTOLE = "limit.disease.diastole";
    // 更多检查结果
    public static final String LIMIT_DISEASE_MOREEXAMINE = "limit.disease.moreExamin";
    // alt
    public static final String LIMIT_DISEASE_ALT = "limt.disease.alt";
    // ast
    public static final String LIMIT_DISEASE_AST = "limt.disease.ast";
    // hbv_dna
    public static final String LIMIT_DISEASE_HBV_DNA = "limt.disease.hbv.dna";
    // 医嘱小结
    public static final String LIMIT_DISEASE_DOCTORORDER = "limit.disease.doctorOrder";
    // 患者病历，其他过敏药物史、既往病史、家族病史、遗传病
    public static final String LIMIT_DISEASE_OTHERS = "limit.disease.others";
    // 患者病历，其他酒类
    public static final String LIMIT_WINES_OTHERS = "limit.wines.others";
    // 孕期哺乳期年龄段
    public static final String LIMIT_PREGNANCY_AGE = "limit.pregnancy.age";

    /**
     * 获取某项配置
     * LimitValueConfig.getLimitConfig()
     *
     * @param configName LimitValueConfig.LIMIT_*
     *
     * @return
     *

     * @Date 2016年7月1日
     * @since 2.5.0
     */
    public static LimitConfigVO getLimitConfig(String configName) {
        return parseConfigValue(ConfigUtil.getString(configName));
    }

    /**
     * 长度校验
     * LimitValueConfig.validateLimit()
     *
     * @param configName  LimitValueConfig.LIMIT_*
     * @param targetValue
     *
     * @return
     *

     * @Date 2016年7月1日
     * @since 2.5.0
     */
    public static boolean validateLimit(String configName, String targetValue) {

        LimitConfigVO configVO = parseConfigValue(ConfigUtil.getString(configName));
        int max = null == configVO.getMax() ? 0 : configVO.getMax();
        int min = null == configVO.getMin() ? 0 : configVO.getMin();

        if (StringUtils.isBlank(targetValue)) {
            return min == 0;
        }
        String regex = "^[\\s\\S]{" + min + "," + max + "}";

        return targetValue.matches(regex);
    }

    /**
     * 加载某项配置值
     * LimitValueConfig.parseConfigValue()
     *
     * @return EXAMPLE1：
     * 19,1,10
     * max,min,def
     * EXAMPLE2：
     * 19,1
     * max,min
     *

     * @Date 2016年7月1日
     * @since 2.5.0
     */
    private static LimitConfigVO parseConfigValue(String configValue) {

        if (StringUtils.isBlank(configValue)) {
            return new LimitConfigVO();
        }
        String[] configs = configValue.split(",");
        Integer max = Integer.valueOf(configs[0].trim());
        Integer min = Integer.valueOf(configs[1].trim());
        Integer def = configs.length > 2 ? Integer.valueOf(configs[2].trim()) : null;
        return new LimitConfigVO(max, min, def);
    }

    public static void validationString(String configName, String targetValue) throws BusinessException {
        if (!LimitValueConfig.validateLimit(configName, targetValue)) {
            throw new BusinessException(ExceptionCodes.INVALID_CONTENT_LENGTH);
        }
    }

    public static void validationNumber(String configName, Number value) throws BusinessException {
        LimitConfigVO limitConfig = parseConfigValue(ConfigUtil.getString(configName));

        if (Double.compare(value.doubleValue(), limitConfig.getMin()) < 0) {
            throw new BusinessException(ExceptionCodes.INVALID_CONTENT_LENGTH);
        }
        if (Double.compare(value.doubleValue(), limitConfig.getMax()) > 0) {
            throw new BusinessException(ExceptionCodes.INVALID_CONTENT_LENGTH);
        }
    }
}
